package service;

import dao.GoodsDao;
import model.Goods;
import model.Page;

import java.sql.SQLException;
import java.util.List;

public class GoodsService {
    GoodsDao gd = new GoodsDao();

    public List<Goods> getScroll() throws SQLException {
        return gd.getScroll();
    }

    public List<Goods> getHost(int recome) throws SQLException {
        return gd.getHost(recome);
    }

    public Page selectPageByTypeID(int typeID, int pageNumber) throws SQLException {
        Page page = new Page();
        //设置页码
        page.setPageNumber(pageNumber);
        //获取一共好多页
        int totalCount = gd.getGoodsCount(typeID);
        //设置页码和总页数
        page.SetPageSizeAndTotalCount(8, totalCount);
        //获取指定页的商品信息
        List list = gd.selectGoodsByTypeID(typeID, pageNumber, 8);
        //封装到集合
        page.setList(list);
        return page;
    }

    /**
     * 获取指定类别指定页的商品信息
     *
     * @param type       商品的类型 0 全部 1 条幅 2 热销 3 新品
     * @param pageNumber 第几页
     * @return Page
     * @throws SQLException
     */
    public Page selectPageByType(int type, int pageNumber) throws SQLException {
        Page page = new Page();
        //初始化默认一页多少数量
        int pageSize = 8;
        //设置页码
        page.setPageNumber(pageNumber);
        //对页码进行处理 好给Dao操作
        pageNumber = (pageNumber - 1) * pageSize;
        //总数量
        int totalcount;
        //等于0表示获取全部商品数量
        if (type == 0) {
            //有多少商品
            totalcount = gd.getGoodsCount(type);
        } else {
            //获取指定的类型商品数量
            totalcount = new RecommendService().getCountById(type);
        }
        //获取一共好多页
        int totalPage = totalcount >= pageSize ? (int) (Math.ceil((double) totalcount / pageSize)) : 1;

        //防止用户输入的页码大于总页码
        if (page.getPageNumber() > totalPage) {
            page.setPageNumber(totalPage);
            pageNumber = (totalPage - 1) * pageSize;
        }

        //设置一页数量和总数量
        page.SetPageSizeAndTotalCount(pageSize, totalcount);
        //设置总页数
        page.setTotalPage(totalPage);
        List goodsList = null;
        try {
            //获取指定页的商品信息
            goodsList = gd.selectGoodsByType(type, pageNumber, pageSize);
            for (Goods goods : (List<Goods>) goodsList) {
                int goodsId = goods.getId();
                goods.setScroll(this.isScroll(goodsId));
                goods.setNew(this.isNew(goodsId));
                goods.setHot(this.isHot(goodsId));
            }
            //封装到集合
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //封装到集合
        page.setList(goodsList);
        return page;
    }

    public Goods goods_detail(int id) throws SQLException {
        return gd.getGoodsDetail(id);
    }

    /**
     * 判断是不是新品
     *
     * @param goodsId 商品id
     * @return boolean
     */
    public boolean isNew(int goodsId) {
        RecommendService rs = new RecommendService();
        return rs.isNew(goodsId);
    }

    /**
     * 判断是不是热销
     *
     * @param goodsId 商品id
     * @return boolean
     */
    public boolean isHot(int goodsId) {
        RecommendService rs = new RecommendService();
        return rs.isHot(goodsId);
    }

    /**
     * 判断是不是新品
     *
     * @param goodsId 商品id
     * @return boolean
     */
    public boolean isScroll(int goodsId) {
        RecommendService rs = new RecommendService();
        return rs.isScroll(goodsId);
    }


    /**
     * 查看是否有指定商品
     *
     * @param goodsId 商品的id
     * @return boolean
     */
    public boolean hasGoods(int goodsId) {
        try {
            return gd.hasGoods(goodsId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 添加商品
     *
     * @param goods 商品对象
     * @return boolean
     */
    public boolean addGoods(Goods goods) {
        try {
            return gd.addGoods(goods);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateGoods(Goods goods) {
        try {
            return gd.updateGoods(goods);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteGoodsById(int id) {
        
        try {
            return gd.deleteGoodsById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
